package com.unlcn.erp.hyc.interfaces.backend.service;

import cn.huiyunche.commons.exception.BusinessException;
import com.unlcn.erp.hyc.interfaces.backend.enums.TransitStatusEnum;
import com.unlcn.erp.hyc.interfaces.base.mapper.HycbackMapper;
import com.unlcn.erp.hyc.interfaces.base.mapper.HycwaybilldetailMapper;
import com.unlcn.erp.hyc.interfaces.base.mapper.SoorderMapper;
import com.unlcn.erp.hyc.interfaces.base.mapper.SoshiplineMapper;
import com.unlcn.erp.hyc.interfaces.base.model.Hycback;
import com.unlcn.erp.hyc.interfaces.base.model.Hycwaybilldetail;
import com.unlcn.erp.hyc.interfaces.base.model.Soorder;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 订单运抵返回
 * Created by zhiche024 on 2017/9/14.
 */
@Service
public class BackHYCOrderServiceImpl implements BackHYCOrderService{

    private static final Logger LOGGER = LoggerFactory.getLogger(BackHYCOrderServiceImpl.class);

    @Autowired
    private CallHYCUrlReceiveService callHYCUrlReceiveService;

    @Autowired
    private HycbackMapper hycbackMapper;

    @Autowired
    private SoorderMapper soorderMapper;

    @Autowired
    private SoshiplineMapper soshiplineMapper;

    @Autowired
    private HycwaybilldetailMapper hycwaybilldetailMapper;

    /**
     * 返回给慧运车关于运抵的订单
     * @return
     */
    @Override
    public int backHYCByOrderArrived() throws Exception{

        //1、获取慧运车传来的所有订单
        Boolean isBack = true;
        //(1)查找记录表中的订单
        List<Hycback> hycbackList =  hycbackMapper.getIsNotBack();
        for(int i = 0; i < hycbackList.size(); i++ ){
            Hycback hycback = hycbackList.get(i);
            //获取订单id
            Long ilineid = hycback.getIorderid();
            //查询该定单下的所有子订单
            List<Soorder> soorderList = soorderMapper.getSubsetByIorderID(ilineid);
            for(int j = 0; j < soorderList.size(); j++ ){
                Soorder soorder = soorderList.get(j);
                //2、判断这些子订单对应的运单是否全部运抵
                Short flag = soshiplineMapper.getFlagByOrderId(soorder.getIlineid());
                if(flag != TransitStatusEnum.ARRIVED.getValue()){
                    isBack = false;
                }
            }
        }

        if(isBack){
            //3、将所有的运抵的母订单返回给慧运车

        }
        //4、将返回的订单标记为已读取
        return 0;
    }

    @Override
    public void backHYCByWayBillArrived() throws Exception{
        //检查HYC运单中未回单的运单
        List<Hycwaybilldetail> hycwaybilldetailList = hycwaybilldetailMapper.getIsNotBack();
        for(int i = 0; i < hycwaybilldetailList.size() ; i++ ){
            Boolean isBack = true;
            Hycwaybilldetail hycwaybilldetail = hycwaybilldetailList.get(i);
            //根据运单号查找订单
            List<Soorder> soorderList = soorderMapper.getOrderBywaybill(hycwaybilldetail.getWaybillcode());
            if(CollectionUtils.isNotEmpty(soorderList)){
                for( int j = 0; j <soorderList.size();j++ ){
                    Soorder soorder = soorderList.get(j);
                    if(soorder.getVcorderno().contains(hycwaybilldetail.getOrdercode())){
                        //说明是包含的
                        isBack = this.judgeIsArrived(soorder.getIlineid());
                    }else{
                        isBack = false;
                    }
                }
                if(isBack){
                    hycwaybilldetail.setIsarrived(1L);
                    hycwaybilldetail.setDtarrive(new Date());
                    //调用慧运车接口
                    Boolean success = callHYCUrlReceiveService.backHYCUrlReceive("SUBMITSHIP",hycwaybilldetail);
                    if(success){
                        //将数据修改入到hycwaybilldetail里
                        hycwaybilldetail.setDtback(new Date());
                        hycwaybilldetail.setIsback(1L);
                        hycwaybilldetailMapper.updateByPrimaryKey(hycwaybilldetail);
                    }else{
                        LOGGER.error("BackHYCOrderServiceImpl.backHYCByWayBillArrived error msg: {}");
                        throw new BusinessException("调用接口时出错");
                    }
                    LOGGER.info("该运单已返回运抵"+hycwaybilldetail.getWaybillcode());
                }
            }
        }
    }

    public Boolean judgeIsArrived(Long iorderId ) throws Exception{
        //获取所有子订单
        List<Soorder> soorderList = soorderMapper.getSubsetByIorderID(iorderId);
        if(CollectionUtils.isNotEmpty(soorderList)){
            for(int i = 0; i < soorderList.size(); i++ ){
                Soorder soorder = soorderList.get(i);
                //2、判断这些子订单对应的运单是否全部运抵
                try{
                    Short flag = soshiplineMapper.getFlagByOrderId(soorder.getIlineid());
                    if(flag != TransitStatusEnum.ARRIVED.getValue()){
                        return false;
                    }
                }catch (NullPointerException e){
                    return false;
                }
            }
        }else{
            //说明没有找到子订单，直接查询改订单做的运单是否已运抵
            try {
                Short flag = soshiplineMapper.getFlagByOrderId(iorderId);
                if(flag != null && !"".equals(flag)){
                    if(flag != TransitStatusEnum.ARRIVED.getValue()){
                        return false;
                    }
                }else{
                    return false;
                }
            }catch (NullPointerException e){
                return false;
            }

        }
        return true;
    }
}
